// index.js
import {
  post,
  get
} from '../../utils/request'
import {
  APIS
} from '../../utils/api'
import { parseData } from '../../utils/util';
// 获取应用实例
const app = getApp()

Page({
  data: {
    tabs: ['球房大厅', '棋牌大厅'],
    endTitle: '预计等待', // 根据剩余时间判断显示文字
    currentIndex: 0,
    info: {},
    // 0 空闲中 1 对局中 2 对局快结束
    roomLost: [],
    statusObj: {},
    system: {},
    currFee: 0,
    isShowTip: false,
    longitude: 0,
    latitude: 0,
    markers: 0
  },
  onLoad() {
    const that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const { longitude, latitude, markers } = res;
        that.setData({
          longitude,
          latitude,
          markers
        });
      }
    })
  },
  onShow() {
    this.getShopInfo();
    this.getList(this.data.currentIndex || 0);
  },
  handelPhone() {
    wx.makePhoneCall({
      phoneNumber: '19527270034'
    })
  },
  handelImg() {
    wx.previewImage({
      current: this.data.info.qrcode,
      urls: [this.data.info.qrcode]
    })
  },
  getShopInfo() {
    const sid = wx.getStorageSync('currentShop');
    if (sid) {
      get(`${APIS.shop}/${sid}`).then(res => {
        const { data, system } = res;
        this.setData({
          info: {
            ...data,
            tag: data.tag.split(','),
          },
          system
        });
      })
    }
  },
  getList(index) {
    const id = wx.getStorageSync('currentShop');
    if (index === 0) {
      get(APIS.ball, { id }).then(res => {
        res.data.forEach(val => {
          let time = 0;
          if (val.status === 1 && val.end) {
            time = val.end - Date.now();
            // 判断time是否小于30分钟
            if (time < 30 * 60 * 1000) {
              this.setData({
                endTitle: '即将空闲'
              })
            }
          } else if (val.status === 1) {
            time = Date.now() - val.start;
          }
          val.duration = parseData(time);
        })
        // 获取最小的curFee
        const min = Math.min(...res.data.map(val => val.currFee));
        this.setData({
          roomLost: res.data,
          isShowRoom: true,
          currFee: res.data.length ? min : 0
        })
      })
    }
    else if (index === 1) {
      get(APIS.card, { id }).then(res => {
        res.data.forEach(val => {
          let time = 0;
          if (val.status === 1 && val.end) {
            time = val.end - Date.now();
            // 判断time是否小于30分钟
            if (time < 30 * 60 * 1000) {
              this.setData({
                endTitle: '即将空闲'
              })
            }
          } else if (val.status === 1) {
            time = Date.now() - val.start;
          }
          val.duration = parseData(time);

        })
        // 获取最小的curFee
        const min = Math.min(...res.data.map(val => val.currFee));
        this.setData({
          roomLost: res.data,
          isShowRoom: true,
          currFee: res.data.length ? min : 0
        })
      })
    }
  },
  handleChange(e) {
    const {
      index
    } = e.target.dataset;
    this.setData({
      currentIndex: index,
      isShowRoom: false
    });
    this.getList(index);
  },
  handleGan() {
    wx.showToast({
      icon: 'none',
      title: '敬请期待~'
    })
  },
  handleShowWifiTip() {
    console.log(1212121);
    this.setData({
      isShowTip: true
    });
  },
  handleCopyPwd() {
    const that = this;
    wx.setClipboardData({
      data: 'shandian8888',
      success(res) {
        wx.getClipboardData({
          success(res) {
            that.setData({
              isShowTip: false
            });
          }
        })
      }
    })
  },
  handleConnectWifi() {
    const that = this;
    wx.connectWifi({
      SSID: 'shandian-5g',
      password: 'shandian8888',
      success(res) {
        that.setData({
          isShowTip: false
        });
      },
      fail(error) {
        console.log(2222, error);
      }
    })

  },
  closePop() {
    this.setData({
      isShowTip: false
    });
  },
  handleTask(e) {
    const { item } = e.target.dataset;
    const type = this.data.currentIndex === 0 ? 'ball' : 'card';
    wx.navigateTo({
      url: `/subPackage/pages/stage?id=${item.id}&type=${type}&inner=1`
    })
  },
  onShareAppMessage() {
    return getApp().globalData.shareMessage
  }
})